package frame.edit 
{
	import debug.ASSERT;
	import flash.events.MouseEvent;
	import frame.IFrame;
	import nsdecl.ns_dialog_service;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_interactive_slot;
	import common.StructTime;
	import proxy.DataAccess;
	import ui.ui_button;
	import ui.ui_combo_box;
	import ui.ui_icon_button;
	import ui.ui_scroll_bar;
	import ui.ui_text;
	import ui.uiGroup;
	import ui.uiRadioGroup;
	import ui.uiUnit;
	import ui.uiWin;
	import ui.UserInput;
	import ui.utils.initUIContainer;
	
	/**
	 * 编辑班次界面
	 * @author anod
	 */
	public class RecordTime extends EditPage implements IFrame 
	{
		use namespace ns_dialog_service;
		
		private var mFrame:FRecordTime;
		private var mCurIdx:int;
		
		public function RecordTime() 
		{
			mFrame = new FRecordTime();
			addChild( mFrame );
			mCurIdx = -1;
			super();
			
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			closeButton.click = "switchChild";
			finishButton.click = "switchChild";
			
			incButton.click = "adjustSize";
			decButton.click = "adjustSize";
			
			okButton.click = "addEditingTimeseg";
		}
		
		public function setupDataMonitor():void 
		{
			timeList.data		= DataAccess.instance.queryTimesegInterf().vname;
			timeList.handler 	= "listTimeseg";
		}
		
		/** 点击+/-按钮，调整工作组人数 */
		ns_interactive_slot function adjustSize( button:ui_icon_button ):void
		{
			var size_now:int = parseInt( sizeText.text );
			( ( incButton == button ) && ++size_now ) || --size_now;
			if ( size_now < 1 ) return;
			sizeText.text = size_now.toString();
		}
		
		/** 点击“新增班次”，加入当前班次 */
		ns_interactive_slot function addEditingTimeseg( button:ui_button ):void
		{
			if ( nameText.length == 0 ) 
			{
				notify( "请至少输入班次名字" );
				return;
			}
			
			var timeseg:StructTime 	= new StructTime( -1 );
			timeseg.name 			= nameText.text;
			timeseg.startHour 		= startHour.data[ startHour.currentKey ];
			timeseg.startMinute 	= startMinute.data[ startMinute.currentKey ];
			timeseg.spendHours 		= spendHour.data[ spendHour.currentKey ];
			timeseg.spendMinutes 	= spendMinute.data[ spendMinute.currentKey ];
			timeseg.size 			= int( sizeText.text );
			
			var result:int;
			if ( mCurIdx < 0 )
			{
				result = DataAccess.instance.queryTimesegInterf().insert( timeseg );
				if ( result < 0 ) notify( "和已有记录相同，无法加入" );
			}
			else 
			{
				var editing:StructTime = timeList.getDataByKey( mCurIdx );
				result = DataAccess.instance.queryTimesegInterf().update( editing.ID, timeseg );
				if ( result < 0 ) notify( "记录出错，请重新输入" );
			}
			if ( result >= 0 ) ns_interactive_slot::exitEditing( button );
		}
		
		/** 显示班次内容 */
		ns_group_handler function listTimeseg( entry:uiUnit, timeseg:StructTime ):void
		{
			var content:UTimeEntry 					= new UTimeEntry();
			content.iNameText.component.text 		= timeseg.name;
			content.iStartText.component.text 		= formatTime(timeseg.startHour) + ":" + formatTime(timeseg.startMinute);
			content.iEndText.component.text 		= formatTime(timeseg.endHour) + ":" + formatTime(timeseg.endMinute);
			content.iSizeText.component.text 		= timeseg.size;
			content.iEditButton.component.click		= "editThisSeg";
			content.iDeleteButton.component.click 	= "delThisSeg";
			
			initUIContainer( content );
			entry.addChild( content );
			
			function formatTime( v:int ):String
			{
				return String("0" + v).slice( -2);
			}
		}
		
		/** 点击“编辑”按钮，编辑当前班次 */
		ns_interactive_slot function editThisSeg( button:ui_button ):void
		{
			var entry:uiUnit 		= button.parent.parent.parent as uiUnit;
			var time:StructTime 	= entry.data;
			mCurIdx 				= entry.key;
			okButton.text 			= "保存编辑";
			finishButton.click 		= "exitEditing";
			nameText.text 			= time.name;
			sizeText.text			= time.size.toString();
			
			startHour.selectLabel( time.startHour );
			startMinute.selectLabel( time.startMinute );
			spendHour.selectLabel( time.spendHours );
			spendMinute.selectLabel( time.spendMinutes );
		}
		
		/** 点击“结束输入”，取消本次输入内容 */
		ns_interactive_slot function exitEditing( button:ui_button ):void
		{
			nameText.text 			= "";
			sizeText.text			= String(1);
			okButton.text			= "新增班次";
			finishButton.click		= "switchChild";
			mCurIdx					= -1;
			
			startHour.selectLabel( 0 );
			startMinute.selectLabel( 0 );
			spendHour.selectLabel( 0 );
			spendMinute.selectLabel( 0 );
		}
		
		/** 点击“删除”，输入本班次 */
		ns_interactive_slot function delThisSeg( button:ui_button ):void
		{
			var entry:uiUnit 	= button.parent.parent.parent as uiUnit;
			var time:StructTime = entry.data;
			
			var result:int = DataAccess.instance.queryTimesegInterf().remove( time.ID );
			ASSERT( result >= 0 );
		}
		
		//交互组件定义
		//------------------------------------
		public function get closeButton():ui_icon_button
		{
			return mFrame.iCloseButton.component;
		}
		public function get timeList():uiGroup
		{
			return mFrame.iTimeList.component;
		}
		public function get scrollBar():ui_scroll_bar
		{
			return mFrame.iScrollBar.component;
		}
		public function get nameText():UserInput
		{
			return mFrame.iNameText.component;
		}
		public function get startHour():ui_combo_box
		{
			return mFrame.iStartHour.component;
		}
		public function get startMinute():ui_combo_box
		{
			return mFrame.iStartMinute.component;
		}
		public function get spendHour():ui_combo_box
		{
			return mFrame.iSpendHour.component;
		}
		public function get spendMinute():ui_combo_box
		{
			return mFrame.iSpendMinute.component;
		}
		public function get incButton():ui_icon_button
		{
			return mFrame.iIncButton.component;
		}
		public function get decButton():ui_icon_button
		{
			return mFrame.iDecButton.component;
		}
		public function get okButton():ui_button
		{
			return mFrame.iOkButton.component;
		}
		public function get finishButton():ui_button
		{
			return mFrame.iFinishButton.component;
		}
		public function get sizeText():ui_text
		{
			return mFrame.iSizeText.component;
		}
	}
}